package com.cs.bd.ad.http.bean;

import android.content.Context;
import android.text.TextUtils;
import com.cs.bd.utils.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class BaseResponseBean
{
    public static final String NORMAL_FLOW_CHANNEL = "1";
    private String mUser = "";
    private String mBuychanneltype = "";
    private String mIPLocal;
    private int mNoad = 0;
    private String mIPAddress;

    public static BaseResponseBean parseBaseResponseBeanJSONObject(Context context, int virtualModuleId, JSONObject json)
    {
        if ((json == null) || (json.length() <= 0)) {
            return null;
        }
        BaseResponseBean baseResponseBean = new BaseResponseBean();
        JSONObject uflagJson = json.optJSONObject("uflag");
        if (uflagJson != null)
        {
            baseResponseBean.setUser(uflagJson.optString("user", ""));
            baseResponseBean.setBuychanneltype(uflagJson.optString("buychanneltype", ""));
            baseResponseBean.mIPLocal = uflagJson.optString("local");
            baseResponseBean.mNoad = uflagJson.optInt("noad");
            baseResponseBean.mIPAddress = uflagJson.optString("ip");
//            if (LogUtils.isShowLog()) {
//                LogUtils.d("Ad_SDK", "Your ip address is " + baseResponseBean.mIPAddress);
//            }
//            AdAvoider.getInstance(context).detect(new Object[] { baseResponseBean.mIPLocal, Integer.valueOf(baseResponseBean.mNoad) });
        }
//        if (LogUtils.isShowLog()) {
//            LogUtils.d("Ad_SDK", "[vmId:" + virtualModuleId + "]BaseResponseBean(mUser=" + baseResponseBean.getUser() + " mBuychanneltype=" + baseResponseBean.getBuychanneltype() + ")");
//        }
        return baseResponseBean;
    }

    public String getBuychanneltype()
    {
        return this.mBuychanneltype;
    }

    public void setBuychanneltype(String mBuychanneltype)
    {
        this.mBuychanneltype = mBuychanneltype;
    }

    public String getUser()
    {
        return this.mUser;
    }

    public void setUser(String mUser)
    {
        this.mUser = mUser;
    }

    public String getIPLocal()
    {
        return this.mIPLocal;
    }

    public int getNoad()
    {
        return this.mNoad;
    }

    public boolean isNormalChannel()
    {
        return "1".equals(this.mBuychanneltype);
    }

    public boolean saveSelfDataToSdcard(int virtualModuleId)
    {
        if ((TextUtils.isEmpty(this.mUser)) && (TextUtils.isEmpty(this.mBuychanneltype))) {
            return false;
        }
        JSONObject cacheDataJsonObject = null;
        JSONObject uflagJson = new JSONObject();
        try
        {
            uflagJson.put("user", this.mUser);
            uflagJson.put("buychanneltype", this.mBuychanneltype);
            uflagJson.put("local", this.mIPLocal);
            uflagJson.put("noad", this.mNoad);
            uflagJson.put("ip", this.mIPAddress);
            cacheDataJsonObject = new JSONObject();
            cacheDataJsonObject.put("uflag", uflagJson);

            cacheDataJsonObject.put("saveDataTime", System.currentTimeMillis());
        }
        catch (JSONException e1)
        {
            e1.printStackTrace();
        }
        if ((null == cacheDataJsonObject) || (cacheDataJsonObject.length() < 1)) {
            return false;
        }
        try
        {
//            return FileCacheUtils.saveCacheDataToSdcard(getCacheFileName(virtualModuleId), StringUtils.toString(cacheDataJsonObject), true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    public static String getCacheFileName(int virtualModuleId)
    {
        return "BaseResponseBean-" + virtualModuleId;
    }

    public static BaseResponseBean getBaseResponseBeanFromCacheData(Context context, int virtualModuleId)
    {
//        String adControlCacheData = FileCacheUtils.readCacheDataToString(getCacheFileName(virtualModuleId), true);
//        if (!TextUtils.isEmpty(adControlCacheData)) {
//            try
//            {
//                return parseBaseResponseBeanJSONObject(context, virtualModuleId, new JSONObject(adControlCacheData));
//            }
//            catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//        }
        return null;
    }
}
